<?php
/**
 * 功能: 用户管理
 * User: 文丹
 * Date: 2018/8/6 0006
 * Time: 10:30
 */

namespace app\admin\controller;
use app\base\controller\Power;
use think\Db;


class User extends Power{
    protected $beforeActionList = [
        'first'=> ['except'=>'search_users,search_profit,partner,search_partner'],

    ];
    protected function first(){
        $result = $this->testing();
    }
    /**
     * 用户列表
     */
    public function member(){
        $users  = Db::name('user')->order('id desc')->paginate(10,false,['query'=>request()->param()])
        ->each(function($item,$key){
            if(!empty($item['parent_id'])){
                $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
            }
            return $item;
        });
        $count = Db::name('user')->count();
        $this->assign([
            'users'=>$users,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="8">暂无数据</td></tr>'
        ]);
        return $this->fetch('user:member-list');
    }

    /**
     * 设为合伙人
     */
    public function become_partner(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'用户id必须为正整数！','ok'=>false]);
        }
        $res = Db::name('user')->where('id',$id)->update(['status'=>2,'is_partner'=>1,'partner_time'=>time()]);
        if($res){
            return json(['msg'=>'已经设为合伙人！','ok'=>true]);
        }else{
            return json(['msg'=>'设置失败！','ok'=>false]);
        }

    }

    /**
     * 会员搜索
     */
    public function search_users(){
        $opt     = intval(input('opt'));
        $datemin = trim(input('datemin'));
        $datemax = trim(input('datemax'));
        $phone   = trim(input('phone'));
        if($opt==1){
            //注册时间
            if(empty($datemin)&&empty($datemax)){
                return json(['msg'=>'搜索条件不能为空！','ok'=>false]);
            }
            if(!empty($datemin)&& !empty($datemax)){
                $users  = Db::name('user')
                    ->whereTime('add_time','>=',$datemin)
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->paginate(10,false,['query'=>request()->param()])
                    ->each(function($item,$key){
                        if(!empty($item['parent_id'])){
                            $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                        }
                        return $item;
                    });
                $count = Db::name('user')
                    ->whereTime('add_time','>=',$datemin)
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->count();
                if(!empty($users[0])){
                    $this->assign([
                        'users'=>$users,
                        'count'=>$count
                    ]);
                    return $this->fetch('user:member-list',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

                }else{
                    return json(['msg'=>'没有符合条件的结果','ok'=>false]);
                }

            }elseif(!empty($datemin)&& empty($datemax)){
                $users  = Db::name('user')
                    ->whereTime('add_time','>=',$datemin)
                    ->paginate(10,false,['query'=>request()->param()])
                    ->each(function($item,$key){
                        if(!empty($item['parent_id'])){
                            $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                        }
                        return $item;
                    });
                $count = Db::name('user')
                    ->whereTime('add_time','>=',$datemin)
                    ->count();
                if(!empty($users[0])){
                    $this->assign([
                        'users'=>$users,
                        'count'=>$count
                    ]);
                    return $this->fetch('user:member-list',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

                }else{
                    return json(['msg'=>'没有符合条件的结果','ok'=>false]);
                }
            }elseif(empty($datemin)&& !empty($datemax)){
                $users  = Db::name('user')
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->paginate(10,false,['query'=>request()->param()])
                    ->each(function($item,$key){
                        if(!empty($item['parent_id'])){
                            $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                        }
                        return $item;
                    });
                $count = Db::name('user')
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->count();
                if(!empty($users[0])){
                    $this->assign([
                        'users'=>$users,
                        'count'=>$count
                    ]);
                    return $this->fetch('user:member-list',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

                }else{
                    return json(['msg'=>'没有符合条件的结果','ok'=>false]);
                }
            }
        }elseif($opt==2){
            //电话号搜索
            if(empty($phone)){
                return json(['msg'=>'搜索条件不能为空','ok'=>false]);
            }
            $users  = Db::name('user')
                ->where('phone','like','%'.$phone.'%')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function($item,$key){
                    if(!empty($item['parent_id'])){
                        $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                    }
                    return $item;
                });
            $count = Db::name('user')
                ->where('phone','like','%'.$phone.'%')
                ->count();
            if(!empty($users[0])){
                $this->assign([
                    'users'=>$users,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-list',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }

    }

    /**
     * 用户收益报表
     */
    public function profit(){
        $profit  = Db::name('profit')->order('id desc')->paginate(10,false,['query'=>request()->param()])
            ->each(function($item,$key){
            $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
                return $item;
            });
        $count   = Db::name('profit')->count();
        $this->assign([
            'profit'=>$profit,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="6">暂无数据</td></tr>'
        ]);
        return $this->fetch('user:member-profit');

    }

    /**
     * 用户记录搜索
     */
    public function search_profit(){
        $datemin = trim(input('datemin'));
        $datemax = trim(input('datemax'));
        $phone   = trim(input('phone'));
        if(empty($datemin)&&empty($datemax)&&empty($phone)){
            return json(['msg'=>'搜索添加不能为空','ok'=>false]);
        }
        if(!empty($datemin)&& !empty($datemax)&& !empty($phone)){
            $profit  = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->whereTime('p.time','>=',$datemin)
                ->whereTime('p.time','<=',strtotime(date($datemax." 23:59:59")))
                ->where('u.phone','like','%'.$phone.'%')
                ->field('p.*,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            $count   = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->whereTime('p.time','>=',$datemin)
                ->whereTime('p.time','<=',strtotime(date($datemax." 23:59:59")))
                ->where('u.phone','like','%'.$phone.'%')
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }elseif(!empty($datemin)&& !empty($datemax)&& empty($phone)){
            $profit  = Db::name('profit')
                ->whereTime('time','>=',$datemin)
                ->whereTime('time','<=',strtotime(date($datemax." 23:59:59")))
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function($item,$key){
                    $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
                    return $item;
                });
            $count   = Db::name('profit')
                ->whereTime('time','>=',$datemin)
                ->whereTime('time','<=',strtotime(date($datemax." 23:59:59")))
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }elseif(!empty($datemin)&& empty($datemax)&& !empty($phone)){
            $profit  = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->whereTime('p.time','>=',$datemin)
                ->where('u.phone','like','%'.$phone.'%')
                ->field('p.*,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            $count   = Db::name('profit')
                    ->alias('p')
                    ->join('user u','p.user_id = u.id')
                    ->whereTime('p.time','>=',$datemin)
                    ->where('u.phone','like','%'.$phone.'%')
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }elseif(empty($datemin)&& !empty($datemax)&& !empty($phone)){
            $profit  = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->whereTime('p.time','<=',strtotime(date($datemax." 23:59:59")))
                ->where('u.phone','like','%'.$phone.'%')
                ->field('p.*,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);
            $count   = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->whereTime('p.time','<=',strtotime(date($datemax." 23:59:59")))
                ->where('u.phone','like','%'.$phone.'%')
                ->field('p.*,u.phone')
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }elseif(!empty($datemin)&& empty($datemax)&& empty($phone)){
            $profit  = Db::name('profit')
                ->whereTime('time','>=',$datemin)
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function($item,$key){
                    $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
                    return $item;
                });
            $count   = Db::name('profit')
                ->whereTime('time','>=',$datemin)
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }elseif(empty($datemin)&& !empty($datemax)&& empty($phone)){
            $profit  = Db::name('profit')
                ->whereTime('time','<=',strtotime(date($datemax." 23:59:59")))
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function($item,$key){
                    $item['phone'] = Db::name('user')->where('id',$item['user_id'])->value('phone');
                    return $item;
                });
            $count  = Db::name('profit')
                ->whereTime('time','<=',strtotime(date($datemax." 23:59:59")))
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }
        }elseif(empty($datemin)&& empty($datemax)&& !empty($phone)){
            $profit  = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->where('u.phone','like','%'.$phone.'%')
                ->field('p.*,u.phone')
                ->paginate(10,false,['query'=>request()->param()]);

            $count   = Db::name('profit')
                ->alias('p')
                ->join('user u','p.user_id = u.id')
                ->where('u.phone','like','%'.$phone.'%')
                ->field('p.*,u.phone')
                ->count();
            if(!empty($profit[0])){
                $this->assign([
                    'profit'=>$profit,
                    'count'=>$count
                ]);
                return $this->fetch('user:member-profit',['datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);
            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }
        }
    }

    /**
     * 收益记录删除
     */
    public function profit_del(){
        $id = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        $result = Db::name('profit')->where('id',$id)->delete();
        if($result){
            return json(['msg'=>'删除成功','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败','ok'=>false]);
        }

    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 用户提现申请
     */
    public function apply(){
        $applies = Db::name('cash')->order('id desc')->paginate(10,false,['query'=>request()->param()])
            ->each(function($item,$key){
                $user = Db::name('user')->where('id',$item['user_id'])->find();
                $item['phone'] = $user['phone'];
                $item['commission'] = $user['commission'];
                $item['ali_account'] = $user['ali_account'];
                return $item;
            });
        $count   = Db::name('cash')->count();
        $this->assign([
            'applies'=>$applies,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="7">暂无数据</td></tr>'
        ]);
        return $this->fetch('user:member-apply');
    }

    /**
     * 申请搜索
     */
    public function user_apply(){
        $phone = trim(input('phone'));
        $logmin = trim(input('logmin'));
        $logmax = trim(input('logmax'));
        $opt = intval(input('opt'));
        $state = intval(input('state'));
        if ($opt == 1) {
            //提现状态搜索
            $applies = Db::name('cash')
                ->where('state', $state)
                ->paginate(10, false, ['query' => request()->param()])
                ->each(function ($item, $key) {
                    $user = Db::name('user')->where('id',$item['user_id'])->find();
                    $item['phone'] = $user['phone'];
                    $item['commission'] = $user['commission'];
                    $item['ali_account'] = $user['ali_account'];
                    return $item;
                });
            $count = Db::name('cash')->where('state', $state)->count();
           /* dump($applies);
            die;*/
            if (!empty($applies[0])) {
                $this->assign([
                    'applies' => $applies,
                    'count' => $count
                ]);
                return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

            } else {
                return json(['msg' => '没有符合条件的结果', 'ok' => false]);
            }
        } elseif ($opt == 2) {
            //用户申请时间
            if (empty($phone) && empty($logmin) && empty($logmax)) {
                return json(['msg' => '搜索条件不能为空', 'ok' => false]);
            }
            if (!empty($phone) && !empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->where('u.phone', $phone)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->where('u.phone', $phone)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (empty($phone) && !empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (!empty($phone) && empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (!empty($phone) && !empty($logmin) && empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->where('u.phone', $phone)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->where('u.phone', $phone)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (!empty($phone) && empty($logmin) && empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (empty($phone) && !empty($logmin) && empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (empty($phone) && empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            }
        }elseif($opt==3){
            //提现成功时间
            if (empty($phone) && empty($logmin) && empty($logmax)) {
                return json(['msg' => '搜索条件不能为空', 'ok' => false]);
            }
            if (!empty($phone) && !empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->whereTime('c.deal_time', '<=', $logmax)
                    ->where('u.phone', $phone)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->whereTime('c.deal_time', '<=', $logmax)
                    ->where('u.phone', $phone)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (empty($phone) && !empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->whereTime('c.deal_time', '<=', $logmax)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->whereTime('c.deal_time', '<=', $logmax)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (!empty($phone) && empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->whereTime('c.deal_time', '<=', $logmax)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (!empty($phone) && !empty($logmin) && empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->where('u.phone', $phone)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->where('u.phone', $phone)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (!empty($phone) && empty($logmin) && empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->where('u.phone', $phone)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (empty($phone) && !empty($logmin) && empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '>=', $logmin)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '>=', $logmin)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            } elseif (empty($phone) && empty($logmin) && !empty($logmax)) {
                $applies = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.deal_time', '<=', $logmax)
                    ->field('u.phone,u.commission,u.ali_account,c.*')
                    ->paginate(10, false, ['query' => request()->param()]);

                $count = Db::name('cash')
                    ->alias('c')
                    ->join('user u', 'c.user_id = u.id')
                    ->whereTime('c.apply_time', '<=', $logmax)
                    ->count();
                if (!empty($applies[0])) {
                    $this->assign([
                        'applies' => $applies,
                        'count' => $count
                    ]);
                    return $this->fetch('user:member-apply', ['phone' => $phone, 'logmin' => $logmin, 'logmax' => $logmax, 'opt' => $opt, 'state' => $state]);

                } else {
                    return json(['msg' => '没有符合条件的结果', 'ok' => false]);
                }
            }
        }

    }

    /**
     * 申请通过
     */
    public function adopt(){
        $id = intval(input('id'));
        $user_id = intval(input('user_id'));
        $amount = intval(input('amount'));
        $remark = trim(input('value'));
        $arr = [
            'user_id'=>$user_id,
            'commission'=>$amount,
            'state'=>2,//1收入，2支出
            'time'=>time(),
            'remark'=>'用户提现'
        ];

        // 启动事务
        Db::startTrans();
        try{
            $result = Db::name('cash')->where('id',$id)->update(['state'=>3,'deal_time'=>time(),'remark'=>$remark]);
            Db::table('profit')->insert($arr);
            // 提交事务
            Db::commit();
            return json(['msg'=>'修改成功','ok'=>true]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['msg'=>'修改失败','ok'=>false]);
        }

    }
    /**
     * 申请拒绝
     */
    public function refuse(){
        $id = intval(input('id'));
        $remark = trim(input('value'));
        $user_id = intval(input('user_id'));
        $amount = intval(input('amount'));

        // 启动事务
        Db::startTrans();
        try{
            $result = Db::name('cash')->where('id',$id)->update(['state'=>2,'remark'=>$remark]);
              Db::table('user')->where('id',$user_id)->setInc('commission',$amount);
            // 提交事务
            Db::commit();
            return json(['msg'=>'修改成功','ok'=>true]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['msg'=>'修改失败','ok'=>false]);
        }

    }
    /**
     * 申请删除
     */
    public function apply_del(){
        $id = intval(input('id'));

        $result = Db::name('cash')->where('id',$id)->delete();
        if($result){
            return json(['msg'=>'已成功','ok'=>true]);
        }else{
            return json(['msg'=>'删除失败','ok'=>false]);
        }

    }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * 合伙人
 */
    public function partner(){
        $users  = Db::name('user')
            ->where('is_partner',1)
            ->order('id desc')
            ->paginate(10,false,['query'=>request()->param()])
            ->each(function($item,$key){
                if(!empty($item['parent_id'])){
                    $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                }
                return $item;
            });
        $count = Db::name('user')->count();
        $this->assign([
            'users'=>$users,
            'count'=>$count,
            'empty'=>'<tr class="text-c empty"><td colspan="8">暂无数据</td></tr>'
        ]);
        return $this->fetch('user:partner');
    }

    /**
     * 修改邀请码
     */
    public function getMark(){
        $id = intval(input('id'));
        $mark = trim(input('mark'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        if(empty($mark)){
            return json(['msg'=>'邀请码不能为空','ok'=>false]);
        }
        $check = Db::name('user')->whereNotIn('id',$id)->where('mark',$mark)->find();
        $plat  = Db::name('plat')->where('mark',$mark)->find();
        if($check ||$plat){
            return json(['msg'=>'该邀请码已经存在','ok'=>false]);
        }else{
            $res = Db::name('user')->where('id',$id)->update(['mark'=>$mark,'code_img'=>'']);
            if($res){
                return json(['msg'=>'修改成功','ok'=>true,'mark'=>$mark]);
            }else{
                return json(['msg'=>'修改失败','ok'=>false]);
            }
        }

    }
    /**
     * 会员搜索
     */
    public function search_partner(){
        $opt     = intval(input('opt'));
        $datemin = trim(input('datemin'));
        $datemax = trim(input('datemax'));
        $phone   = trim(input('phone'));
        if($opt==1){
            //注册时间
            if(empty($datemin)&&empty($datemax)){
                return json(['msg'=>'搜索条件不能为空！','ok'=>false]);
            }
            if(!empty($datemin)&& !empty($datemax)){
                $users  = Db::name('user')
                    ->where('is_partner',1)
                    ->whereTime('add_time','>=',$datemin)
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->paginate(10,false,['query'=>request()->param()])
                    ->each(function($item,$key){
                        if(!empty($item['parent_id'])){
                            $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                        }
                        return $item;
                    });
                $count = Db::name('user')
                    ->where('is_partner',1)
                    ->whereTime('add_time','>=',$datemin)
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->count();
                if(!empty($users[0])){
                    $this->assign([
                        'users'=>$users,
                        'count'=>$count
                    ]);
                    return $this->fetch('user:partner',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

                }else{
                    return json(['msg'=>'没有符合条件的结果','ok'=>false]);
                }

            }elseif(!empty($datemin)&& empty($datemax)){
                $users  = Db::name('user')
                    ->where('is_partner',1)
                    ->whereTime('add_time','>=',$datemin)
                    ->paginate(10,false,['query'=>request()->param()])
                    ->each(function($item,$key){
                        if(!empty($item['parent_id'])){
                            $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                        }
                        return $item;
                    });
                $count = Db::name('user')
                    ->where('is_partner',1)
                    ->whereTime('add_time','>=',$datemin)
                    ->count();
                if(!empty($users[0])){
                    $this->assign([
                        'users'=>$users,
                        'count'=>$count
                    ]);
                    return $this->fetch('user:partner',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

                }else{
                    return json(['msg'=>'没有符合条件的结果','ok'=>false]);
                }
            }elseif(empty($datemin)&& !empty($datemax)){
                $users  = Db::name('user')
                    ->where('is_partner',1)
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->paginate(10,false,['query'=>request()->param()])
                    ->each(function($item,$key){
                        if(!empty($item['parent_id'])){
                            $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                        }
                        return $item;
                    });
                $count = Db::name('user')
                    ->where('is_partner',1)
                    ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                    ->count();
                if(!empty($users[0])){
                    $this->assign([
                        'users'=>$users,
                        'count'=>$count
                    ]);
                    return $this->fetch('user:partner',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

                }else{
                    return json(['msg'=>'没有符合条件的结果','ok'=>false]);
                }
            }
        }elseif($opt==2){
            //电话号搜索
            if(empty($phone)){
                return json(['msg'=>'搜索条件不能为空','ok'=>false]);
            }
            $users  = Db::name('user')
                ->where('is_partner',1)
                ->where('phone','like','%'.$phone.'%')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function($item,$key){
                    if(!empty($item['parent_id'])){
                        $item['parentName'] = Db::name('user')->where('id',$item['parent_id'])->value('nickname');
                    }
                    return $item;
                });
            $count = Db::name('user')
                ->where('is_partner',1)
                ->whereTime('add_time','<=',strtotime(date($datemax." 23:59:59")))
                ->count();
            if(!empty($users[0])){
                $this->assign([
                    'users'=>$users,
                    'count'=>$count
                ]);
                return $this->fetch('user:partner',['opt'=>$opt,'datemin'=>$datemin,'datemax'=>$datemax,'phone'=>$phone]);

            }else{
                return json(['msg'=>'没有符合条件的结果','ok'=>false]);
            }

        }

    }

    /**
     * 生成二维码
     */
    public function create(){
        $id   = intval(input('id'));
        if($id<=0){
            return json(['msg'=>'id必须为正整数','ok'=>false]);
        }
        $mark = trim(input('mark'));
        if(empty($mark)){
            return json(['msg'=>'邀请码不能为空','ok'=>false]);
        }
        $alias = randLetter().$id;
        $code = json_decode(scerweima2($mark,$alias),true);

        if($code['ok']){
            $result = Db::name('user')->where('id',$id)->update(['code_img'=>$code['msg']]);
            if($result){
                return json(['msg'=>'生成二维码成功','ok'=>true]);
            }else{
                return json(['msg'=>'生成二维码失败','ok'=>false]);
            }
        }else{
            return json(['msg'=>$code['msg'],'ok'=>false]);
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 用户 - 返利设置
     */
    public function rebate(){
        $type = Db::name('user_status')->select();
        $rule = [];
        if($type){
            foreach($type as $k=>$v){
                $rule[$k] = $v;
                $res = Db::name('rebate_rule')->where('us_id',$v['id'])->find();
                $rule[$k]['r_id']   = $res['id'];
                $rule[$k]['self']   = $res['self'];
                $rule[$k]['level1'] = $res['level1'];
                $rule[$k]['level2'] = $res['level2'];
                $rule[$k]['sub'] = $res['sub'];
            }
        }

        $this->assign([
            'rule'=>$rule,
        ]);
        return view('');

    }
    /**
     * 返利规则设置
     */
    public function set_rule(){
        $self   = input('self');
        $sub    = input('sub');
        $level1 = input('level1');
        $level2 = input('level2');
        $us_id  = input('id');
        $id     = input('r_id');

       if($level1>$sub||$level2>$sub){
           return json(['msg'=>'提成量不可大于提成分佣','ok'=>false]);
       }
        $data = [
            'self'=>$self,
            'sub'=>$sub,
            'level1'=>$level1,
            'level2'=>$level2,
            'us_id'=>$us_id,
        ];
        $check = Db::name('rebate_rule')->where('id',$id)->where('us_id',$us_id)->find();
        if($check){
            $res = Db::name('rebate_rule')->where('id',$id)->update($data);

        }else{
            $res = Db::name('rebate_rule')->insert($data);
        }
        if($res){
            return json(['msg'=>'设置成功','ok'=>true]);
        }else{
            return json(['msg'=>'设置失败','ok'=>false]);
        }

    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 签到设置
     */
    public function sign_rule(){
        $res = Db::name('sign_rule')->find();
        $this->assign([
            'result'=>$res,
        ]);
        return view('');
    }

    public function set_sign(){
        $intro    = trim(input('intro'));
        $integral = intval(input('integral'));
        $score    = intval(input('score'));
        $money    = intval(input('money'));
        $image    = trim(input('image'));
        if(empty($intro)||empty($integral)||empty($score)||empty($money)||empty($image)){
            return json(['msg'=>'请填写完整的数据！','ok'=>false]);
        }
        $data = [
            'intro'=>$intro,
            'integral'=>$integral,
            'score'=>$score,
            'money'=>$money,
            'image'=>$image,
        ];
        $check = Db::name('sign_rule')->find();
        if($check){
            $res = Db::name('sign_rule')->where('id',$check['id'])->update($data);
        }else{
            $res = Db::name('sign_rule')->insert($data);
        }
        if($res){
            return json(['msg'=>'设置成功','ok'=>true]);
        }else{
            return json(['msg'=>'设置失败','ok'=>false]);
        }
    }
}